package com.gwz.cardserver.sockets;

import com.alibaba.fastjson.JSON;
import com.gwz.cardserver.consts.SocketConst;
import com.gwz.cardserver.dto.ReqBase;
import com.gwz.cardserver.global.GlobalCache;
import com.gwz.cardserver.logic.IHandler;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author Gjing
 * <p>
 * netty服务端处理器
 **/
@Slf4j
@Component
@ChannelHandler.Sharable
public class NettyServerHandler extends ChannelInboundHandlerAdapter {

    @Resource(name = "AccountHandler")
    private IHandler accountHandler;
    @Resource(name = "matchHandler")
    private IHandler matchHandler;
    @Resource(name = "chatHandler")
    private IHandler chatHandler;
    @Resource(name = "fightHandler")
    private IHandler fightHandler;
    @Autowired
    private GlobalCache globalCache;

    /**
     * 客户端连接会触发
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("建立客户端连接......{}", ctx.name());
        globalCache.removeAccount(ctx);
    }

    /**
     * 客户端发消息会触发
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        log.info("服务器收到消息: {}", msg.toString());
        ReqBase reqBase = JSON.parseObject(msg.toString(), ReqBase.class);
        switch (reqBase.getCode()) {
            case SocketConst.ACCOUNT:
                accountHandler.channelRead(ctx, reqBase);
                break;
            case SocketConst.MATCH:
                matchHandler.channelRead(ctx, reqBase);
                break;
            case SocketConst.CHAT:
                chatHandler.channelRead(ctx, reqBase);
                break;
            case SocketConst.FIGHT:
                fightHandler.channelRead(ctx, reqBase);
                break;
        }
    }

    /**
     * 发生异常触发
     */
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        if (cause instanceof IllegalArgumentException) {
            // 关闭的连接不进行发送
            if (ctx.isRemoved()) {
                return;
            }
            String message = cause.getMessage();
            ctx.write(message);
            ctx.flush();
        } else {
            cause.printStackTrace();
        }
    }


    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        super.handlerRemoved(ctx);
        String userIdBYChannel = globalCache.getUserIdBYChannel(ctx);
        log.info("{}用户移除连接:{}", userIdBYChannel);
        fightHandler.handlerRemoved(ctx);
        // 聊天移除
        chatHandler.handlerRemoved(ctx);
        // 比赛移除
        matchHandler.handlerRemoved(ctx);
        // 移除用户
        accountHandler.handlerRemoved(ctx);

    }
}